package com.njupt.wuaiagent.controller;

import com.njupt.wuaiagent.agent.WuManus;
import com.njupt.wuaiagent.app.LoveApp;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.ai.tool.ToolCallbacks;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.awt.*;
import java.io.IOException;

/**
 * @Author: wujiaming
 * @CreateTime: 2025/7/20 19:32
 * @Description:
 * @Version: 1.0
 */


@RestController
@RequestMapping("/ai")
public class AiController {


    @Resource
    private LoveApp loveApp;


    @Resource
    private ToolCallback[] allTools;

    @Resource
    private ChatModel dashscopeChatModel;


    /**
     * 同步调用AI 恋爱应用大师
     *
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping("love_app/chat/sync")
    public String doChatWithLoveAppSyn(String message, String chatId) {
        return loveApp.doChat(message, chatId);

    }

    /**
     * SSE 流式调用 AI恋爱应用大师
     * 定义HTTP响应头以什么样的方式返回
     *
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "love_app/chat/sse", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> doChatWithLoveAppSSE(String message, String chatId) {
        return loveApp.doChatByStream(message, chatId);

    }

    /**
     * SSE 流式调用 AI恋爱应用大师
     * 使用ServerSentEvent<String>包装一层
     * @param message
     * @param chatId
     * @return
     */
    @GetMapping(value = "love_app/chat/server_sent_event")
    public Flux<ServerSentEvent<String>> doChatWithLoveAppServerSentEvent(String message, String chatId) {
        return loveApp.doChatByStream(message, chatId)
                .map(chunk -> {
                            return ServerSentEvent.<String>builder()
                                    .data(chunk)
                                    .build();
                        }
                );

    }

    @GetMapping(value = "love_app/chat/sse/emitter")
    public SseEmitter doChatWithLoveAppEmitter(String message, String chatId) {
      //创建一个超时时间较长的SseEmitter
        SseEmitter sseEmitter = new SseEmitter(180000L);
        loveApp.doChatByStream(message,chatId)
                //获取Flux数据流，并且直接订阅
                .subscribe(chunk ->{
                    try {
                        //对数据流中每一个数据chunk进行发送
                        sseEmitter.send(chunk);
                    } catch (IOException e) {
                        //如果有异常那么就进行异常处理
                        sseEmitter.completeWithError(e);
                    }
                }, sseEmitter::completeWithError //处理错处的情况

                 ,sseEmitter::complete);//处理完成的情况
        return sseEmitter;
    }


    /**
     * 流式调用manus超时智能体
     * @param message
     * @return
     */
    @GetMapping("/manus/chat")
    public SseEmitter doChatWithManus(String message){
        WuManus wuManus = new WuManus(allTools,dashscopeChatModel);
        return wuManus.runStream(message);
    }
}
